﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using Intorise.Common;
using System.Net;
using System.IO;

namespace TextTools
{
    public partial class Form1 : Form
    {
        Encoding encoding = Encoding.UTF8;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string text = this.txtSrc.Text;
            byte[] datas = encoding.GetBytes(text);

            this.txtDst.AppendText("\r\n{" + text + "}-> ");
            foreach (byte b in datas)
            {
                if (checkBoxString.Checked)
                {
                    if (checkBoxHex.Checked)
                    {
                        this.txtDst.AppendText("\"0x" + b.ToString("X2") + "\", ");
                    }
                    else
                    {
                        this.txtDst.AppendText("\"" + b.ToString("X2") + "\", ");
                    }
                }
                else
                {
                    if (checkBoxHex.Checked)
                    {
                        this.txtDst.AppendText("0x" + b.ToString("X2") + ", ");
                    }
                    else
                    {
                        this.txtDst.AppendText(b.ToString("X2") + ", ");
                    }
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string text = this.txtSrc.Text;
            string[] numbers = text.Split(new char[] { '"', ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
            if (numbers.Length == 0)
            {
                return;
            }
            List<byte> list = new List<byte>();
            byte data = 0;
            string character;
            foreach (string num in numbers)
            {
                character = num;
                if (character.StartsWith("0x"))
                {
                    character = num.Replace("0x", "");
                }
                if (byte.TryParse(character, NumberStyles.HexNumber, null, out data))
                {
                    list.Add(data);
                }
            }
            this.txtDst.AppendText(Tools.ConvertChar(encoding.GetString(list.ToArray())));
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton radioButton = sender as RadioButton;

            switch (radioButton.Name)
            {
                case "radASC":
                    radUTF8.Checked = !radASC.Checked;
                    radGB2312.Checked = !radASC.Checked;
                    encoding = Encoding.ASCII;
                    break;
                case "radUTF8":
                    radASC.Checked = !radUTF8.Checked;
                    radGB2312.Checked = !radUTF8.Checked;
                    encoding = Encoding.UTF8;
                    break;
                case "radGB2312":
                    radUTF8.Checked = !radGB2312.Checked;
                    radASC.Checked = !radGB2312.Checked;
                    encoding = Encoding.GetEncoding("gb2312");
                    break;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string text = this.txtSrc.Text;
            string[] numbers = text.Split(new char[] { '"', ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
            if (numbers.Length == 0)
            {
                return;
            }
            List<string> list = new List<string>();

            foreach (string num in numbers)
            {
                list.Add("0x" + num);
            }
            this.txtDst.AppendText("\r\n{" + text + "}-> ");
            this.txtDst.AppendText(string.Join(",", list.ToArray()));
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int num = 0;
            if (!int.TryParse(this.txtSrc.Text, out num))
            {
                return;
            }

            num = IPAddress.HostToNetworkOrder(num);
            this.txtDst.AppendText(num + ":" + num.ToString("X2").PadLeft(8, '0'));
        }

        private void button5_Click(object sender, EventArgs e)
        {
            int num = 0;
            if (!int.TryParse(this.txtSrc.Text, out num))
            {
                return;
            }

            this.txtDst.AppendText(num + ":" + num.ToString("X2").PadLeft(8, '0'));
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string text = this.txtSrc.Text;
            string[] numbers = text.Split(new char[] { '"', ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
            if (numbers.Length == 0)
            {
                return;
            }
            List<byte> list = new List<byte>();
            byte data = 0;
            string character;
            foreach (string num in numbers)
            {
                character = num;
                if (character.StartsWith("0x"))
                {
                    character = num.Replace("0x", "");
                }
                if (byte.TryParse(character, NumberStyles.HexNumber, null, out data))
                {
                    list.Add(data);
                }
            }


            MemoryStream output = new MemoryStream(list.Count);
            zlib.ZOutputStream outZStream = new zlib.ZOutputStream(output);

            byte[] datas = list.ToArray();
            outZStream.Write(datas, 0, datas.Length);
            outZStream.Flush();

            datas = output.ToArray();
            foreach (byte b in datas)
            {
                this.txtDst.AppendText(b.ToString("X2").ToUpper().PadLeft(2, '0') + " ");
            }
        }
    }
}
